<script setup>

import { onMounted, ref } from "vue"
import { artGetChannelsService, artDelChannelService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import { isLoading, toggleLoading } from '@/composables/loading'
const channelList = ref([])
const getChannelList = async () => {
  toggleLoading()
  const res = await artGetChannelsService()
  channelList.value = res.data
  // console.log(channelList.value);
  toggleLoading()
}
onMounted(async () => {
  await getChannelList()
})
// 编辑表单
const dialog = ref()
// 新增分类
const addChannel = () => {
  dialog.value.open({})
}
//修改分类
const onEditChannel = (row) => {
  // console.log(row, $index)
  dialog.value.open(row)
}
// 删除分类
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannelList()
}
</script>

<template>
  <PageContainer title="文章分类">
    <!-- 定制头 -->
    <template #extra>
      <el-button type="primary" @click="addChannel()">新增分类</el-button>
      <el-button type="danger">移除分类</el-button>
    </template>
    <!-- 定制内容 -->
    <el-table :data="channelList" style="width: 100%" fixed v-loading="isLoading" element-loading-text="加载中...">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column prop="cate_alias" label="分类名" />
      <el-table-column prop="cate_name" label="分类别名" />
      <el-table-column label="操作" width="100">
        <template #default="{ row, $index }">
          <el-button :icon="Edit" circle plain type="primary" @click="onEditChannel(row, $index)"
            size="default"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="onDelChannel(row, $index)"
            size="default"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据" />
      </template>
    </el-table>
  </PageContainer>
<!-- 编辑分类 -->
  <channel-edit ref="dialog" @success="getChannelList()"></channel-edit>
</template>

<style scoped></style>
